package com.sanben.demand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanben.common.util.BeanCopyUtils;
import com.sanben.demand.data.dto.CommentDto;
import com.sanben.demand.data.req.CommentReq;
import com.sanben.demand.data.vo.CommentVo;
import com.sanben.demand.entity.Comment;
import com.sanben.demand.entity.CommentReply;
import com.sanben.demand.mapper.CommentMapper;
import com.sanben.demand.service.ICommentReplyService;
import com.sanben.demand.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cyh
 * @description: 评论表 服务实现类
 * @date: 2020-08-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private ICommentReplyService commentReplyService;

    @Override
    public IPage<CommentVo> page(CommentReq req) {
        Page<CommentVo> page = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<Comment> commentQuery=new LambdaQueryWrapper<>();
        commentQuery.eq(Comment::getDemandId,req.getDemandId());
        List<CommentVo> list = this.baseMapper.page(page, req);
        list.stream().forEach(each->{
            LambdaQueryWrapper<CommentReply> commentReplyQuery=new LambdaQueryWrapper<>();
            commentReplyQuery.eq(CommentReply::getCommentId,each.getId());
            List<CommentReply> commentReplyList=commentReplyService.list(commentReplyQuery);
            each.setCommentReplyList(commentReplyList);
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Boolean save(CommentDto dto) {
        return this.save(BeanCopyUtils.copy(dto, Comment.class));
    }

    @Override
    public Boolean updateById(CommentDto dto) {
        return this.updateById(BeanCopyUtils.copy(dto, Comment.class));
    }


    @Override
    public Boolean deleteLogic(List<Integer> toIntList) {
        return this.removeByIds(toIntList);
    }

    @Override
    public Comment detail(Integer id) {
        return this.getById(id);
    }
}
